package com.cskaoyan.service;

import com.cskaoyan.bean.BaseReq;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.StorageExample;
import com.cskaoyan.bean.storage.bo.StorageReq;
import com.cskaoyan.bean.storage.vo.StorageData;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.util.CskaoyanPageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @HJL
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    private StorageMapper storageMapper;

    @Override
    public StorageData getStorageList(BaseReq req) {
        CskaoyanPageHelper.startPage(req);
        StorageReq storageReq = (StorageReq) req;
        String name = storageReq.getName();
        String key = storageReq.getKey();

        StorageExample example = new StorageExample();
        example.setOrderByClause(storageReq.getSort() + " " + storageReq.getOrder());
        StorageExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);//未删除的
        if (StringUtils.hasText(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (StringUtils.hasText(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Storage> storages = storageMapper.selectByExample(example);

        PageInfo<Storage> pageInfo = new PageInfo<>(storages);
        long total = pageInfo.getTotal();

        StorageData storageData = new StorageData();
        storageData.setTotal(total);
        storageData.setItems(storages);
        return storageData;
    }

    /**
     * 删除存储对象
     * @param storage
     * @return
     */
    @Override//逻辑删除 总是成功
    public boolean deleteStorage(Storage storage) {
        Storage st = new Storage();
        st.setId(storage.getId());
        st.setDeleted(true);

        storageMapper.updateByPrimaryKeySelective(st);
        return true;
    }

    /**
     * 登记上传图片到数据库
     * @param storage
     * @return
     */
    @Override//0 ok 1 fail
    public int createStorage(Storage storage) {
        Date now = new Date();
        storage.setAddTime(now);
        storage.setUpdateTime(now);
        int update = storageMapper.insertSelective(storage);
        return update > 0 ? 0 : 1;
    }

    /**
     * 跟新图片信息，实际上只有name能修改 和 id有用
     * @param storage
     * @return
     */
    @Override
    public int updateStorage(Storage storage) {
        if (storage == null || storage.getId() == null) throw new IllegalArgumentException("参数为空或没有id");
        storage.setUpdateTime(new Date());
        int update = storageMapper.updateByPrimaryKeySelective(storage);
        return update > 0 ? 0 : 1;
    }

    @Override
    public boolean isDeleted(Integer id, String avatar) {
        if (id != null) {
            Storage storage = storageMapper.selectByPrimaryKey(id);
            if (storage == null) {
                return true;//不存在的头像
            }
            return storage.getDeleted();
        }
        if (StringUtils.hasText(avatar)) {
            StorageExample example = new StorageExample();
            StorageExample.Criteria criteria = example.createCriteria();
            criteria.andUrlEqualTo(avatar);
            List<Storage> storages = storageMapper.selectByExample(example);
            if (storages.size() == 0) {
                return true;//不存在的头像
            }
            return storages.get(0).getDeleted();
        }
        return true;//不存在
    }
}
